package ua.org.nuos.sdms.clientgui.client.view.selectGroup;

import com.vaadin.ui.*;
import ua.org.nuos.sdms.clientgui.client.components.factory.ButtonFactory;
import ua.org.nuos.sdms.clientgui.client.components.factory.SeparatorFactory;
import ua.org.nuos.sdms.clientgui.client.components.factory.WindowFactory;
import ua.org.nuos.sdms.clientgui.client.controller.GlobalBoard;
import ua.org.nuos.sdms.clientgui.client.events.ChangeCurrentGroupEvent;
import ua.org.nuos.sdms.clientgui.client.util.DescriptionBuilder;
import ua.org.nuos.sdms.clientgui.server.AppProxy;
import ua.org.nuos.sdms.middle.entity.Group;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: dio
 * Date: 06.03.12
 * Time: 19:15
 * To change this template use File | Settings | File Templates.
 */
public class SelectGroupComponent extends VerticalLayout {

    public static final String ID = SelectGroupComponent.class.getSimpleName();

    public SelectGroupComponent() {
        init();
    }

    private void init() {
        setSizeFull();
        AppProxy.getInstance().getComponentLocator().addContainer(ID, this);

        List<Group> groups = AppProxy.getInstance().getServiceLocator().getClientBean().findCurrentUserGroups();
        if (groups.isEmpty()) {
            Label message = new Label("Вы не состоите ни в одной группе." +
                    "<br />Вступите в группу либо создайте новую.");
            message.setContentMode(Label.CONTENT_XHTML);
            addComponent(message);
        } else {
            Panel panel = new Panel();
            panel.setStyleName("borderless light");
            panel.setScrollable(true);
            panel.setSizeFull();
            for (Group group : groups) {
                Button button = createGroupButton(group);
                panel.addComponent(button);
                panel.addComponent(SeparatorFactory.getHorizontal());
            }
            addComponent(panel);
            setExpandRatio(panel, 1.f);
        }

        Button close = WindowFactory.getCloseButton(
                AppProxy.getInstance().getComponentLocator().getWindow(SelectGroupWindow.ID).getWindow(), "Не стоит");
        addComponent(close);
        setComponentAlignment(close, Alignment.BOTTOM_RIGHT);
    }

    private Button createGroupButton(Group g) {
        Button b = ButtonFactory.getButton(g.getName(), GlobalBoard.ID, new ChangeCurrentGroupEvent(g));
        b.setStyleName("borderless");
        b.addStyleName("button_b_caption");
        b.setDescription(DescriptionBuilder.buildGroupDescription(g));

        return b;
    }
}
